//
//  LoginViewController.mm
//  iDashan
//
//  Created by Huahang Liu on 10/16/09.
//  Copyright 2009 University of Southern California. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "LoginViewController.h"
#import "DashanEngine.h"
#import "NSData+Base64.h"

@implementation LoginViewController

@synthesize delegate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (IBAction)login:(id)sender {
    
//    [self dismissModalViewControllerAnimated:YES];
//    if ([delegate respondsToSelector:@selector(loginViewController:didFinishLoginWithInfo:)]) {
//        NSDictionary* loginInfo = [[[NSDictionary alloc] initWithObjectsAndKeys:@"huahang", @"username", nil] autorelease];
//        [delegate loginViewController:self didFinishLoginWithInfo:loginInfo];
//    }

    NSLog(@"Begin Login");
    
    DashanEngine* dashanEngine = [[DashanEngine alloc] initWithBaseAddr:@"http://dashan-net.appspot.com/"];

    NSString*     method_name = [NSString stringWithFormat:@"getsalt"];
    NSArray*      param_names = [NSArray arrayWithObjects:@"username",       nil];
    NSArray*      param_vals  = [NSArray arrayWithObjects:@"test@gmail.com", nil];
    NSDictionary* params      = [NSDictionary dictionaryWithObjects:param_vals forKeys:param_names];
    [dashanEngine callMethod:method_name withParameters:params];    
    
    NSString*      pswd = [NSString stringWithFormat:@"1234"];
    NSString*      salt = [NSString stringWithFormat:@"EdkUSqsFq2"];
    NSData*   pswd_data = [pswd dataUsingEncoding:NSUTF8StringEncoding];
    NSData*   salt_data = [salt dataUsingEncoding:NSUTF8StringEncoding];

    // Compute SHA1
    CC_SHA1_CTX   sha1_ctx;
    unsigned char sha1_md[CC_SHA1_DIGEST_LENGTH];
    CC_SHA1_Init(&sha1_ctx);
    CC_SHA1_Update(&sha1_ctx, pswd_data.bytes, pswd_data.length);
    CC_SHA1_Update(&sha1_ctx, salt_data.bytes, salt_data.length);
    CC_SHA1_Final(sha1_md, &sha1_ctx);
    NSData* md_data = [NSData dataWithBytes:sha1_md length:CC_SHA1_DIGEST_LENGTH];
    NSString* md_base64 = [md_data base64EncodingWithLineLength:0];
    NSLog(@"%@", md_base64);

    [dashanEngine release];

    NSLog(@"End Login");
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark -
#pragma mark <UITextFieldDelegate> Methods

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
